provider "alicloud" {
  profile = "default"
  version = "1.153.0"
}

data "alicloud_instance_types" "instance_type" {
  instance_type_family = "ecs.n4"
  cpu_core_count       = "1"
  memory_size          = "2"
}

data "alicloud_zones" "default" {
  available_resource_creation = "VSwitch"
  available_instance_type     = data.alicloud_instance_types.instance_type.instance_types[0].id
}

resource "alicloud_vpc" "main" {
  vpc_name   = "vpc-${var.short_name}"
  cidr_block = "10.1.0.0/21"
}

resource "alicloud_vswitch" "main" {
  vpc_id     = alicloud_vpc.main.id
  cidr_block = "10.1.1.0/24"
  zone_id    = data.alicloud_zones.default.zones[0].id
}

resource "alicloud_security_group" "group" {
  name        = "group-${var.short_name}"
  description = "New security group"
  vpc_id      = alicloud_vpc.main.id
}

resource "alicloud_security_group_rule" "allow_http_80" {
  type              = "ingress"
  ip_protocol       = "tcp"
  nic_type          = var.nic_type
  policy            = "accept"
  port_range        = "80/80"
  priority          = 1
  security_group_id = alicloud_security_group.group.id
  cidr_ip           = "0.0.0.0/0"
}

resource "alicloud_security_group_rule" "allow_https_22" {
  type              = "ingress"
  ip_protocol       = "tcp"
  nic_type          = var.nic_type
  policy            = "accept"
  port_range        = "22/22"
  priority          = 1
  security_group_id = alicloud_security_group.group.id
  cidr_ip           = "0.0.0.0/0"
}

resource "alicloud_ecs_key_pair" "key_pair" {
  key_pair_name = var.key_name
  public_key = file("${path.module}/jenkins.pem")
}

resource "alicloud_instance" "instance" {
  instance_name   = "${var.short_name}-${var.role}-${format(var.count_format, count.index + 1)}"
  host_name       = "${var.short_name}-${var.role}-${format(var.count_format, count.index + 1)}"
  image_id        = var.image_id
  instance_type   = data.alicloud_instance_types.instance_type.instance_types[0].id
  count           = var.number
  security_groups = alicloud_security_group.group.*.id

  internet_charge_type       = var.internet_charge_type
  internet_max_bandwidth_out = var.internet_max_bandwidth_out

  password = var.ecs_password
  key_name = alicloud_ecs_key_pair.key_pair.id

  instance_charge_type = "PostPaid"
  system_disk_category = var.disk_category
  vswitch_id = alicloud_vswitch.main.id

  user_data = <<EOF
#cloud-config

# add each entry to ~/.ssh/authorized_keys for the configured user or the
# first user defined in the user definition directive.
ssh_authorized_keys:
  - ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAGEA3FSyQwBI6Z+nCSjUUk8EEAnnkhXlukKoUPND/RRClWz2s5TCzIkd3Ou5+Cyz71X0XmazM3l5WgeErvtIwQMyT1KjNoMhoJMrJnWqQPOt5Q8zWd9qG7PBl9+eiH5qV7NZ mykey@host
  - ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA3I7VUf2l5gSn5uavROsc5HRDpZdQueUq5ozemNSj8T7enqKHOEaFoU2VoPgGEWC9RyzSQVeyD6s7APMcE82EtmW4skVEgEGSbDc1pvxzxtchBj78hJP6Cf5TCMFSXw+Fz5rF1dR23QDbN1mkHs7adr8GW4kSWqU7Q7NDwfIrJJtO7Hi42GyXtvEONHbiRPOe8stqUly7MvUoN+5kfjBM8Qqpfl2+FNhTYWpMfYdPUnE7u536WqzFmsaqJctz3gBxH9Ex7dFtrxR4qiqEr9Qtlu3xGn7Bw07/+i1D+ey3ONkZLN+LQ714cgj8fRS4Hj29SCmXp5Kt5/82cD/VN3NtHw== smoser@brickies
write_files:
  - content: |
      15 * * * * root ship_logs
    path: /etc/crontab
    append: true
bootcmd:
  - echo "Hello World. The time is now $(date -R)!" | tee /root/userdata_test.txt
runcmd:
  - rpm -ivh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
  - yum install -y mysql-community-client.x86_64
  - yum install -y wget git
  - wget http://gosspublic.alicdn.com/ossutil/1.7.8/ossutil64
  - chmod 755 ossutil64
  - mv ossutil64 /usr/local/bin/ossutil
EOF

  tags = {
    role = var.role
  }
}

module "main" {
  source = "../"

  content = templatefile("${path.module}/config.tpl", {

    accessKeyId     = "accessKeyId_123"
    secretAccessKey = "secretAccessKey_123"

    region          = "cn-hangzhou"
    vpc_id          = alicloud_vpc.main.id
    vsw_id          = "vsw-123"
    sg_id           = "sg_123"
    lb_id           = "lb_123"

    oss_bucket      = "oss_b1"

    sls_projectName = ""
    sls_logStoreName= ""

    sms_signName    = "小云云"

    app_id          = ""
    xj_app_id       = ""

    tsdb_id         = "tsdb_id"
    tsdb_host       = "tsdb_host"

    rds_id          = "rds_id"
    rds_host        = alicloud_instance.instance.0.public_ip
    rds_port        = 3306
    rds_user        = "root"
    rds_password    = "123456"
    rds_database    = "t_user"

    redis_id        = "redis_123"
    redis_host      = alicloud_instance.instance.0.public_ip
    redis_port      = 6379
    redis_password  = "root@123"

    server_name     = "xiaoqi"
  })

  private_key       = file("${path.module}/id_dsa")
  host              = alicloud_instance.instance.0.public_ip
}

output "hostname_list" {
  value = join(",", alicloud_instance.instance.*.instance_name)
}

output "ecs_ids" {
  value = join(",", alicloud_instance.instance.*.id)
}

output "ecs_public_ip_list" {
  value = join(",", alicloud_instance.instance.*.public_ip)
}

output "key_pair" {
  value = jsonencode(alicloud_ecs_key_pair.key_pair.id)
}

output "tags" {
  value = jsonencode(alicloud_instance.instance.*.tags)
}